package com.ptszyxx.popose.module.main.dynamic;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.databinding.FragmentDynamicReplyBinding;
import com.ysg.http.data.AppViewModelFactory;
import com.ptszyxx.popose.module.main.dynamic.adapter.DynamicReplyAdapter;
import com.ysg.http.data.entity.dynamic.CommentEntity;
import com.ysg.http.data.entity.dynamic.ReplyEntity;
import com.ptszyxx.popose.module.main.dynamic.vm.DynamicReplyVM;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.ysg.base.BaseFragment;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.utils.YStringUtil;

import java.util.List;

public class DynamicReplyFragment extends BaseFragment<FragmentDynamicReplyBinding, DynamicReplyVM> {
    private DynamicReplyAdapter adapter;
    private View headerView;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_dynamic_reply;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public DynamicReplyVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(DynamicReplyVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            viewModel.commentEntity = bundle.getParcelable(BundleConstant.ENTITY);
            viewModel.commentId = viewModel.commentEntity.getId()+"";
        }
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestReplyPage(true);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new DynamicReplyAdapter(viewModel);
        YRecyclerViewUtil.init(getContext(), binding.recyclerView, adapter);
        headerView = LayoutInflater.from(getContext()).inflate(R.layout.item_dynamic_comment, null);
        headerView.findViewById(R.id.viewHide).setVisibility(View.VISIBLE);
        adapter.addHeaderView(headerView);
        setHeaderViewUI();

        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.requestReplyPage(true);
            }
        });
        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.requestReplyPage(false);
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<ReplyEntity>>() {
            @Override
            public void onChanged(List<ReplyEntity> list) {
                setAdapterData(list);
            }
        });
        viewModel.uc.onInputClearEvent.observe(this, o -> {
           binding.editCommit.setText("");
        });
    }

    /**
     * 显示头部
     */
    private void setHeaderViewUI() {
        if (viewModel.commentEntity == null){
            return;
        }
        CommentEntity item = viewModel.commentEntity;
        //头像
        ImageView heardImage = headerView.findViewById(R.id.ivAvatar);
        if (YStringUtil.isNotEmpty(item.getPic())){
            YImageUtil.show(heardImage, item.getPic());
        }
        //昵称
        TextView commName = headerView.findViewById(R.id.tvName);
        commName.setText(item.getNick() + "");
        //性别
        ImageView ivSex = headerView.findViewById(R.id.ivSex);
        if (YStringUtil.eq(2, item.getSex())) {
            ivSex.setImageResource(R.mipmap.ic_girl);
        } else {
            ivSex.setImageResource(R.mipmap.ic_boy);
        }
        //点赞
        ImageView ivPinglDZ = headerView.findViewById(R.id.ivLike);
        if (YStringUtil.eq(1, item.getDzstate())) {
            ivPinglDZ.setImageResource(R.mipmap.ic_like_select);
        } else {
            ivPinglDZ.setImageResource(R.mipmap.ic_like_unselect);
        }
        TextView tvPinglunDZNum = headerView.findViewById(R.id.tvLike);
        tvPinglunDZNum.setText(item.getNum() + "");
        TextView tvPinluntime = headerView.findViewById(R.id.tvTime);
        tvPinluntime.setText(item.getCreatetime() + "");
        TextView commCount = headerView.findViewById(R.id.tvContent);
        commCount.setText(item.getNeirong() + "");

        View commt_linyout = headerView.findViewById(R.id.viewReply);
        commt_linyout.setVisibility(View.GONE);
    }

    private void setAdapterData(List<ReplyEntity> list){
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
        if (YCollectionUtil.isNotEmpty(list)){
            if (1 == viewModel.pageNo){
                adapter.setList(list);
            }else{
                adapter.addData(list);
            }
        }else{
            binding.refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

}
